;
(function () {

  'use strict';

  angular.module('services.customActivity.summary')
    .service('summaryHealthMaxService', summaryHealthMaxService)
    .service('summaryHealthMinService', summaryHealthMinService)
    .service('summaryHealthAvgService', summaryHealthAvgService);

  var option = {
    tooltip: {
      show: false
    },
    legend: {
      data: [],
      show: false
    },
    toolbox: {
      show: false
    },
    series: [
      {
        type: 'pie',
        radius: ['50%', '68%'],
        center: ['50%', '50%'],
        clickable: false,
        data: [
          {
            value: 20,
            name: '',
            itemStyle: {
              normal: {
                label: {textStyle: {color: '#607d8b', fontSize: 12}},
                borderWidth: '2',
                borderColor: "#fff",
                labelLine: {lineStyle: {color: '#607d8b'}},
                color: '#4caf50'
              },
              emphasis: {borderWidth: '2', borderColor: "#fff", color: 'transparent'}
            }
          },
          {
            value: 20,
            name: '',
            itemStyle: {
              normal: {
                label: {textStyle: {color: '#607d8b', fontSize: 12}},
                borderWidth: '2',
                borderColor: "#fff",
                labelLine: {lineStyle: {color: '#607d8b'}},
                color: '#4caf50'
              },
              emphasis: {borderWidth: '2', borderColor: "#fff", color: 'transparent'}
            }
          },
          {
            value: 20,
            name: '',
            itemStyle: {
              normal: {
                label: {textStyle: {color: '#607d8b', fontSize: 12}},
                borderWidth: '2',
                borderColor: "#fff",
                labelLine: {lineStyle: {color: '#607d8b'}},
                color: '#4caf50'
              },
              emphasis: {borderWidth: '2', borderColor: "#fff", color: 'transparent'}
            }
          },
          {
            value: 20,
            name: '',
            itemStyle: {
              normal: {
                label: {textStyle: {color: '#607d8b', fontSize: 12}},
                borderWidth: '2',
                borderColor: "#fff",
                labelLine: {lineStyle: {color: '#607d8b'}},
                color: '#4caf50'
              },
              emphasis: {borderWidth: '2', borderColor: "#fff", color: 'transparent'}
            }
          },
          {
            value: 20,
            name: '',
            itemStyle: {
              normal: {
                label: {textStyle: {color: '#607d8b', fontSize: 12}},
                borderWidth: '2',
                borderColor: "#fff",
                labelLine: {lineStyle: {color: '#607d8b'}},
                color: '#4caf50'
              },
              emphasis: {borderWidth: '2', borderColor: "#fff", color: 'transparent'}
            }
          }
        ]
      }
    ]
  };

  function switchColor(state, obj) {
    switch (state) {
      case 1:
        obj.color = '#4caf50';
        break;
      case 2:
        obj.color = '#2196f3';
        break;
      case 3:
        obj.color = '#ffeb3b';
        break;
      case 4:
        obj.color = '#f44336';
        break;
    }
  }

  function initChart(that, result, element) {

    that.sumHealth = result.upay;

    switch (result.upayState) {
      case 1:
        that.sumHealthState = '优';
        break;
      case 2:
        that.sumHealthState = '良';
        break;
      case 3:
        that.sumHealthState = '中';
        break;
      case 4:
        that.sumHealthState = '差';
        break;
    }

    switchColor(result.tmallState, option.series[0].data[2].itemStyle.normal);
    switchColor(result.scpayState, option.series[0].data[0].itemStyle.normal);
    switchColor(result.bankState, option.series[0].data[1].itemStyle.normal);
    switchColor(result.bankState, option.series[0].data[1].itemStyle.normal);
    switchColor(result.bankState, option.series[0].data[1].itemStyle.normal);

    option.series[0].data[0].name = '移动商城\n' + (result.scpay || 0) + '%';
    option.series[0].data[1].name = '银行总对总\n' + (result.bank || 0) + '%';
    option.series[0].data[2].name = '天猫旗舰店\n' + (result.tmall || 0) + '%';
    option.series[0].data[3].name = '有价卡\n' + (result.vces || 0) + '%';
    option.series[0].data[4].name = '灵犀语音\n' + (result.lingxi || 0) + '%';

    (echarts.init(element)).setOption(option);
  }

  function summaryHealthMaxService(Restangular, generatorSummaryPageService) {
    var that = this;
    this.init = function (element) {

      if (generatorSummaryPageService.pageInfo.preview) {
        setTimeout(function () {
          that.loading = true;
          initChart(that, {
            upay: 1.00,
            upayState: 4,
            tmall: 1.00,
            tmallState: 4,
            scpay: 1.00,
            scpayState: 4,
            bank: 1.00,
            bankState: 4,
            vces: 1.00,
            vcesState: 4,
            lingxi: 1.00,
            lingxiState: 4
          }, element);
        }, 0);
        return;
      }

      that.loading = false;
      Restangular.all('sas/customActivity/summary/health/max/query').post({
        summaryId: generatorSummaryPageService.pageInfo.summaryId
      }).then(function (result) {
        that.loading = true;
        if (result.state === '10000') {
          initChart(that, result.data, element);
        }
      });
    };
  }

  summaryHealthMaxService.$inject = ['Restangular', 'generatorSummaryPageService'];

  function summaryHealthMinService(Restangular, generatorSummaryPageService) {
    var that = this;
    this.init = function (element) {

      if (generatorSummaryPageService.pageInfo.preview) {
        setTimeout(function () {
          that.loading = true;
          initChart(that, {
            upay: 1.00,
            upayState: 4,
            tmall: 1.00,
            tmallState: 4,
            scpay: 1.00,
            scpayState: 4,
            bank: 1.00,
            bankState: 4,
            vces: 1.00,
            vcesState: 4,
            lingxi: 1.00,
            lingxiState: 4
          }, element);
        }, 0);
        return;
      }

      that.loading = false;
      Restangular.all('sas/customActivity/summary/health/min/query').post({
        summaryId: generatorSummaryPageService.pageInfo.summaryId
      }).then(function (result) {
        that.loading = true;
        if (result.state === '10000') {
          initChart(that, result.data, element);
        }
      });
    };
  }

  summaryHealthMinService.$inject = ['Restangular', 'generatorSummaryPageService'];

  function summaryHealthAvgService(Restangular, generatorSummaryPageService) {
    var that = this;
    this.init = function (element) {

      if (generatorSummaryPageService.pageInfo.preview) {
        setTimeout(function () {
          that.loading = true;
          initChart(that, {
            upay: 1.00,
            upayState: 4,
            tmall: 1.00,
            tmallState: 4,
            scpay: 1.00,
            scpayState: 4,
            bank: 1.00,
            bankState: 4,
            vces: 1.00,
            vcesState: 4,
            lingxi: 1.00,
            lingxiState: 4
          }, element);
        }, 0);
        return;
      }

      that.loading = false;
      Restangular.all('sas/customActivity/summary/health/avg/query').post({
        summaryId: generatorSummaryPageService.pageInfo.summaryId
      }).then(function (result) {
        that.loading = true;
        if (result.state === '10000') {
          initChart(that, result.data, element);
        }
      });
    };
  }

  summaryHealthAvgService.$inject = ['Restangular', 'generatorSummaryPageService'];
})();